#include "MainWindow.h"
#include "ui_MainWindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //初始化
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            s[i][j]=0;
        }
    }
    button = new QPushButton("开始游戏",this);
    button->setGeometry(120,800,400,100);//从(120,800)做一个400*100的按钮
    //随机函数
    qsrand(uint(QTime(0,0,0).secsTo(QTime::currentTime())));

    connect(button,SIGNAL(clicked()),this,SLOT(slotStart()));

}
MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    p.setBrush(Qt::blue);
    p.setFont(QFont("微软雅黑",40,1400,false));

    QString strscore;
    p.drawText(QPoint(40,120),"分数: "+QString::number(score));

    for (int i=0;i<4;i++) {
        for (int j=0;j<4;j++) {
            p.setPen(Qt::transparent);//?
            if(s[i][j]==0){
                p.setBrush(Qt::gray);
                p.drawRect(i*120+80,j*120+240,110,110);
            }
            else if (s[i][j]==2) {
                p.setBrush(Qt::red);
                p.drawRect(i*120+80,j*120+240,110,110);
                p.setPen(Qt::black);
                p.setFont(QFont("微软雅黑",20,1400,false));
                //在方块中 绘制Text， 并且文本位置位于中部
                p.drawText(QRectF(i*120+80,j*120+240,110,110),QString::number(2),QTextOption(Qt::AlignCenter));
            }
            else if (s[i][j]==4) {
                p.setBrush(Qt::darkRed);
                p.drawRect(i*120+80,j*120+240,110,110);
                p.setPen(Qt::black);
                p.setFont(QFont("微软雅黑",20,1400,false));
                p.drawText(QRectF(i*120+80,j*120+240,110,110),QString::number(4),QTextOption(Qt::AlignCenter));
            }
            else if (s[i][j]==8) {
                p.setBrush(Qt::green);
                p.drawRect(i*120+80,j*120+240,110,110);
                p.setPen(Qt::black);
                p.setFont(QFont("微软雅黑",20,1400,false));
                p.drawText(QRectF(i*120+80,j*120+240,110,110),QString::number(8),QTextOption(Qt::AlignCenter));
            }
            else if (s[i][j]==16) {
                p.setBrush(Qt::darkGreen);
                p.drawRect(i*120+80,j*120+240,110,110);
                p.setPen(Qt::black);
                p.setFont(QFont("微软雅黑",20,1400,false));
                p.drawText(QRectF(i*120+80,j*120+240,110,110),QString::number(16),QTextOption(Qt::AlignCenter));
            }
            else if (s[i][j]==32) {
                p.setBrush(Qt::yellow);
                p.drawRect(i*120+80,j*120+240,110,110);
                p.setPen(Qt::black);
                p.setFont(QFont("微软雅黑",20,1400,false));
                p.drawText(QRectF(i*120+80,j*120+240,110,110),QString::number(32),QTextOption(Qt::AlignCenter));
            }
            else if (s[i][j]==64) {
                p.setBrush(Qt::darkYellow);
                p.drawRect(i*120+80,j*120+240,110,110);
                p.setPen(Qt::black);
                p.setFont(QFont("微软雅黑",20,1400,false));
                p.drawText(QRectF(i*120+80,j*120+240,110,110),QString::number(64),QTextOption(Qt::AlignCenter));
            }
            else if (s[i][j]==128) {
                p.setBrush(Qt::cyan);
                p.drawRect(i*120+80,j*120+240,110,110);
                p.setPen(Qt::black);
                p.setFont(QFont("微软雅黑",20,1400,false));
                p.drawText(QRectF(i*120+80,j*120+240,110,110),QString::number(128),QTextOption(Qt::AlignCenter));
            }
            else if (s[i][j]==256) {
                p.setBrush(Qt::darkCyan);
                p.drawRect(i*120+80,j*120+240,110,110);
                p.setPen(Qt::black);
                p.setFont(QFont("微软雅黑",20,1400,false));
                p.drawText(QRectF(i*120+80,j*120+240,110,110),QString::number(256),QTextOption(Qt::AlignCenter));
            }
            else if (s[i][j]==512) {
                p.setBrush(Qt::magenta);
                p.drawRect(i*120+80,j*120+240,110,110);
                p.setPen(Qt::black);
                p.setFont(QFont("微软雅黑",20,1400,false));
                p.drawText(QRectF(i*120+80,j*120+240,110,110),QString::number(512),QTextOption(Qt::AlignCenter));
            }
            else if (s[i][j]==1024) {
                p.setBrush(Qt::darkMagenta);
                p.drawRect(i*120+80,j*120+240,110,110);
                p.setPen(Qt::black);
                p.setFont(QFont("微软雅黑",20,1400,false));
                p.drawText(QRectF(i*120+80,j*120+240,110,110),QString::number(1024),QTextOption(Qt::AlignCenter));
            }
            else if (s[i][j]==2048) {
                p.setBrush(Qt::blue);
                p.drawRect(i*120+80,j*120+240,110,110);
                p.setPen(Qt::black);
                p.setFont(QFont("微软雅黑",20,1400,false));
                p.drawText(QRectF(i*120+80,j*120+240,110,110),QString::number(2048),QTextOption(Qt::AlignCenter));
            }
            else{
                p.setBrush(Qt::darkBlue);
                p.drawRect(i*120+80,j*120+240,110,110);
                p.setPen(Qt::black);
                p.setFont(QFont("微软雅黑",20,1400,false));
                p.drawText(QRectF(i*120+80,j*120+240,110,110),QString::number(s[i][j]),QTextOption(Qt::AlignCenter));
            }
        }
    }
}

void MainWindow::keyPressEvent(QKeyEvent *event)
{
    if(!state){
        return ;
    }
        switch (event->key()) {
        case Qt::Key_W:
            PressUp();
            break;

        case Qt::Key_S:
            PressDown();
            break;

        case Qt::Key_A:
            PressLeft();
            break;

        case Qt::Key_D:
            PressRight();
            break;

        default:
            return;//忽略其他按钮
        }
        myRand();
        update();
}
